import { resource } from "shared/component/resource/resource";
import { SkinServiceImpl } from "shared/modules/skin/service/SkinServiceImpl";
/**
 * 皮肤路径
 */
const skinsFolder = game.GetService('ServerStorage').FindFirstChild('skins');
/**
 * 皮肤模板路径
 */
const skintemplateFolder = game.GetService('ServerStorage').FindFirstChild('skintemplate');
export default class SkinServerServiceImpl extends SkinServiceImpl {
    generateAccsForCharacter(characterEntity: Ctn.character.PlayerCharacterEntity) {
        if (!characterEntity.character?.IsA('Model')) {
            throw (characterEntity.character + ' is not a Model');
        }
        this.generateAccsFromTempate(characterEntity.skinPath).forEach(acc => {
            acc.Parent = characterEntity.character;
            const mesh = acc.FindFirstChild('Handle');
            if (!mesh?.IsA('BasePart')) {
                throw (mesh + ' is not a BasePart');
            }
            const avatarpartscalseType = mesh.WaitForChild('AvatarPartScaleType');
            const BodyTypeScale = characterEntity.character?.Humanoid.FindFirstChild('BodyTypeScale');
            let avatarpartscalseTypeValue = "Classic"
            if (BodyTypeScale?.IsA('NumberValue') && BodyTypeScale.Value === 0) {
                avatarpartscalseTypeValue = "Classic"
            } else if (BodyTypeScale?.IsA('NumberValue') && BodyTypeScale.Value === 1) {
                avatarpartscalseTypeValue = "ProportionsNormal"
            }
            if (avatarpartscalseType.IsA('StringValue')) {
                avatarpartscalseType.Value = avatarpartscalseTypeValue
            }
        })
    }
    /**
     * 从模板路径生成皮肤
     * @param templatePath 
     */
    generateAccsFromTempate(templatePath = "C1") {
        const skinTemplateScript = resource.getPath(templatePath, skintemplateFolder);
        if (!skinTemplateScript.IsA('ModuleScript')) {
            throw (skinTemplateScript + ' is not a ModuleScript')
        }
        /**
         * 皮肤模板
         */
        const skinsTemplate = require(skinTemplateScript) as Ctn.skin.Skin[];
        return this.generateAccsFromSkins(skinsTemplate);//从skin数组生成皮肤
    }
    /**
     * 从skin数组生成皮肤
     * @param skinsTemplate 
     */
    generateAccsFromSkins(skinsTemplate: Ctn.skin.Skin[]) {
        /** 皮肤网格数组 */
        const meshsOfSkin = this.generateMeshPartsFromSkins(skinsTemplate)//从skin数组生成网格
        const accs = new Array<Accessory>()
        meshsOfSkin.forEach(mesh => {
            const access = new Instance('Accessory');
            access.Name = "skin_" + mesh.Name
            mesh.Parent = access
            mesh.Name = 'Handle';
            accs.push(access);
        })
        return accs
    }
    /**
     *  从skin数组生成网格
     * @param skinsTemplate 
     * @returns 
     */
    generateMeshPartsFromSkins(skinsTemplate: Ctn.skin.Skin[]) {
        /** 皮肤网格数组 */
        const meshsOfSkin = new Array<MeshPart>()
        skinsTemplate?.forEach(skin => {
            if (!skin.meshPath) {
                throw (skin + ' do not have meshPath')
            }
            const meshFromPath = resource.getPath(skin.meshPath, skinsFolder);
            if (!meshFromPath.IsA('MeshPart')) {
                throw (meshFromPath + ' is not a MeshPart')
            }
            const meshsOfSkinNew = meshFromPath.Clone();
            meshsOfSkinNew.CanCollide = false;
            meshsOfSkinNew.CanTouch = false;
            meshsOfSkinNew.CanQuery = false;
            if (skin.texture) {
                const texture = resource.getPath(skin.texture, game.GetService('ServerStorage')).Clone();
                if (texture) {
                    texture.Parent = meshsOfSkinNew
                }
            }
            if (skin.color)
                meshsOfSkinNew.Color = skin.color
            meshsOfSkin.push(meshsOfSkinNew);
        })
        return meshsOfSkin;
    }
}